package core.jwt;

import core.constants.Constant;
import core.domain.LoginUser;
import core.redis.RedisService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
@Slf4j
public class TokenService {

    @Autowired
    private RedisService redisService;
    public String createJWT(Long userId, String secret, Integer identity, String nickname, String headImage) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", userId);
        String token = JWTUtils.createToken(claims, secret);
        // 将用户信息存入redis, 延长token有效期
        LoginUser loginUser = new LoginUser();
        loginUser.setIdentity(identity);
        loginUser.setNickname(nickname);
        loginUser.setHeadImage(headImage);
        redisService.setCacheObject(Constant.LOGIN_USER + userId,
                loginUser, Constant.TIME_OUT, TimeUnit.MINUTES);
        return token;
    }

    public void extendToken(String token, String secret) {
        Claims claims = getClaims(token, secret);
        if(claims == null) {
            return;
        }
        String userId = claims.get("userId").toString();
        long expire = redisService.getExpire(Constant.LOGIN_USER + userId);
        if(expire < Constant.LIMIT_TIME_OUT) {
            redisService.expire(Constant.LOGIN_USER + userId, Constant.TIME_OUT,
                    TimeUnit.MINUTES);
        }

    }

    public LoginUser getLoginUser(String token, String secret) {
        Claims claims = getClaims(token, secret);
        if(claims == null) {
            return null;
        }
        String userId = claims.get("userId").toString();
        if(userId == null) {
            return null;
        }
        return redisService.getCacheObject(Constant.LOGIN_USER + userId, LoginUser.class);
    }

    public boolean deleteLoginUser(String token, String secret) {
        Claims claims = getClaims(token, secret);
        if(claims == null) {
            return false;
        }
        String userId = claims.get("userId").toString();
        return redisService.deleteObject(Constant.LOGIN_USER + userId);
    }
    private Claims getClaims(String token, String secret) {
        Claims claims;
        try {
            claims = JWTUtils.parseToken(token, secret);
            if(claims == null) {
                log.error("token解析失败");
                return null;
            }
        } catch (Exception e) {
            log.error("token解析失败 {}", token, e);
            return null;
        }
        return claims;
    }

    public void refreshLoginUser(String nickName, String headImage, Long userId) {
        String useKey = Constant.LOGIN_USER + userId;
        LoginUser loginUser = redisService.getCacheObject(useKey, LoginUser.class);
        loginUser.setNickname(nickName);
        loginUser.setHeadImage(headImage);
        redisService.setCacheObject(useKey, loginUser, Constant.TIME_OUT, TimeUnit.MINUTES);
    }
}
